package com.sright.bcard.content.biz.manager.impl;

import com.sright.bcard.common.service.share.dto.pager.Pager;
import com.sright.bcard.content.biz.manager.FavoriteCardManager;
import com.sright.bcard.content.dal.dao.*;
import com.sright.bcard.content.dal.entity.*;
import com.sright.bcard.common.service.share.dto.content.FavoriteCardDTO;
import com.sright.bcard.content.share.request.FavoriteRequest;
import com.sright.bcard.enterprise.dal.dao.CompanyDAO;
import com.sright.bcard.enterprise.dal.dao.EnterpriseDAO;
import com.sright.bcard.enterprise.dal.entity.CompanyDO;
import com.sright.bcard.enterprise.dal.entity.EnterpriseDO;
import com.sright.bcard.user.dal.dao.MemberDAO;
import com.sright.bcard.user.dal.entity.MemberDO;
import com.sright.common.response.SrightResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
@Slf4j
public class FavoriteCardManagerImpl implements FavoriteCardManager {
    @Autowired
    private CardDAO cardDAO;

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private CardUserInfoDAO cardUserInfoDAO;

    @Autowired
    private FavoriteCardDAO favoriteCardDAO;

    @Autowired
    private FavoriteDAO favoriteDAO;

    @Autowired
    private EnterpriseDAO enterpriseDAO;

    @Autowired
    private CardUserCompanyInfoDAO cardUserCompanyInfoDAO;

    @Autowired
    private CompanyDAO companyDAO;

    @Autowired
    private MemberDAO memberDAO;

    @Autowired
    private CardBaseInfoDAO cardBaseInfoDAO;

    @Autowired
    private MaterialDAO materialDAO;

    @Override
    public SrightResponse addFavorite(FavoriteRequest favoriteRequest) {
        CardUserInfoDO cardUserInfoDO = cardUserInfoDAO.selectOneByUserId(favoriteRequest.getUserId());
        //被收藏用户信息
        UserDO userInfo = userDAO.selectById(favoriteRequest.getUserId());

        if (userInfo.getMemberId().equals(favoriteRequest.getMemberId())) {
            return SrightResponse.defaultSuccess();
        }

        FavoriteCardDO favoriteCardDO = new FavoriteCardDO();
        favoriteCardDO.setMemberId(favoriteRequest.getMemberId());
        favoriteCardDO.setFromCardId(favoriteRequest.getCardId());
        favoriteCardDO.setFromUserId(favoriteRequest.getUserId());
        favoriteCardDO.setFromEnterpriseId(userInfo.getEnterpriseId());

        FavoriteCardDO favoriteCardDO1 = favoriteCardDAO.selectByObject(favoriteCardDO);
        if (favoriteCardDO1 == null) {
            favoriteCardDO.setName(cardUserInfoDO.getName());
            favoriteCardDAO.insert(favoriteCardDO);
        } else {
            favoriteCardDAO.updateSelective(favoriteCardDO);
        }

        FavoriteDO favoriteDO = favoriteDAO.selectByMemberIdAndEnterpriseId(favoriteRequest.getMemberId(),userInfo.getEnterpriseId());
        if (favoriteDO == null) {
            favoriteDO = new FavoriteDO();
            favoriteDO.setMemberId(favoriteRequest.getMemberId());
            favoriteDO.setFromEnterpriseId(userInfo.getEnterpriseId());
            favoriteDO.setTotal(1);

            if (favoriteRequest.getEnterpriseId() > 0) {
                EnterpriseDO enterpriseDO = enterpriseDAO.selectById(userInfo.getEnterpriseId());
                favoriteDO.setFromEnterpriseName(enterpriseDO.getName());
            } else {
                favoriteDO.setFromEnterpriseName("个人名片");
            }
            favoriteDAO.insert(favoriteDO);
        } else {
            favoriteDO.setTotal(favoriteDO.getTotal() + 1);
            favoriteDAO.updateAll(favoriteDO);
        }
        return SrightResponse.defaultSuccess();
    }

    @Override
    public SrightResponse getFavoriteCard(Integer page, String keywords, Long memberId) {
        if (StringUtils.isEmpty(keywords)) {
            keywords = null;
        }
        List<FavoriteCardDO> favoriteCardList = favoriteCardDAO.selectPageList((page - 1) * 10,keywords,memberId);
        Integer count = favoriteCardDAO.selectCount(keywords,memberId);

        List<FavoriteCardDTO> favoriteCardDTOS = new ArrayList<>();
        for (FavoriteCardDO favoriteCardDO : favoriteCardList) {
            FavoriteCardDTO favoriteCardDTO = new FavoriteCardDTO();
            favoriteCardDTO.setCardId(favoriteCardDO.getFromCardId());
            favoriteCardDTO.setUserId(favoriteCardDO.getFromUserId());
            favoriteCardDTO.setName(favoriteCardDO.getName());

            UserDO userDO = userDAO.selectById(favoriteCardDO.getFromUserId());
            favoriteCardDTO.setEnterpriseId(userDO.getEnterpriseId());

            CardUserInfoDO cardUserInfoDO = cardUserInfoDAO.selectOneByUserId(favoriteCardDO.getFromUserId());
            favoriteCardDTO.setMobile(cardUserInfoDO.getMobile());
            favoriteCardDTO.setFontColor(cardUserInfoDO.getFontColor());
            List<Long> cardUserInfoIds = Arrays.asList(cardUserInfoDO.getId());
            List<CardUserCompanyInfoDO> companyInfos = cardUserCompanyInfoDAO.selectByCardUserInfoIds(cardUserInfoIds);
            CompanyDO companyDO = companyDAO.selectById(companyInfos.get(0).getCompanyId());
            if(null != companyDO) {
                favoriteCardDTO.setCompany(companyDO.getName());
            }
            favoriteCardDTO.setDepartment(companyInfos.get(0).getDepartment());
            favoriteCardDTO.setJob(companyInfos.get(0).getJob());

            UserDO collectUser = userDAO.selectById(favoriteCardDO.getFromUserId());
            if (collectUser == null) {
                favoriteCardDTO.setAvatar("");
            } else {
                MemberDO memberDO = memberDAO.selectById(collectUser.getMemberId());
                if (memberDO != null) {
                    favoriteCardDTO.setAvatar(memberDO.getAvatarLocal());
                } else {
                    favoriteCardDTO.setAvatar("");
                }
            }

            favoriteCardDTO.setFilePath("");

            CardDO cardDO = cardDAO.selectById(favoriteCardDO.getFromCardId());
            CardBaseInfoDO cardBaseInfoDO = cardBaseInfoDAO.selectById(cardDO.getBaseInfoId());
            //获取封面图
            if (cardBaseInfoDO != null && !cardBaseInfoDO.getMaterialIds().equals("")) {
                String[] materialIds = cardBaseInfoDO.getMaterialIds().split(",");
                List<Long> materialIdList = new ArrayList<>();
                for(String materialId : materialIds) {
                    materialIdList.add(Long.valueOf(materialId));
                }
                if (materialIds.length > 0) {
                    List<MaterialDO> materialDOList = materialDAO.selectByMaterialIdList(materialIdList);
                    if (materialDOList != null && materialDOList.size() > 0) {
                        List<Long> typeList = Arrays.asList(1L,2L,4L,5L);
                        for (MaterialDO materialDO : materialDOList) {
                            if (typeList.contains(cardBaseInfoDO.getTemplateId())) {
                                if (materialDO.getType() == 1 || materialDO.getType() == 2) {
                                    favoriteCardDTO.setFilePath(materialDO.getFilePath());
                                }
                            } else {
                                if (materialDO.getType() == 6) {
                                    favoriteCardDTO.setFilePath(materialDO.getFilePath());
                                }
                            }
                        }
                    }
                }
            }

            favoriteCardDTOS.add(favoriteCardDTO);
        }

        Pager<FavoriteCardDTO> pager = new Pager<>(page , 10, count, favoriteCardDTOS);
        return SrightResponse.defaultSuccess(pager);
    }
}
